<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="ordersQuery.query" class="input-with-select">
            <el-button slot="append" icon="el-icon-search" @click="searchOrder"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="orderList" stripe border>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column prop="order_pay" label="是否付款">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.order_pay == 1">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column prop="create_time" label="下单时间"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)" icon="el-icon-edit" type="primary"></el-button>
            <el-button size="mini" type="success" icon="el-icon-location" @click="showLogistics"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="ordersQuery.pagenum" :page-sizes="[5, 10, 15]" :page-size="ordersQuery.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </el-card>
    <el-dialog title="修改地址" :visible.sync="dialogFormVisible" @close="closeForm">
      <el-form :model="editAddress" ref="editAddressRef" :rules="editAddressRules" label-width="100px">
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader :options="cityData" v-model="editAddress.address1"> </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editAddress.address2" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editForm">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="物流进度" :visible.sync="dialogLogVisible">
      <el-timeline>
        <el-timeline-item v-for="(activity, index) in logisticsInfo" :key="index" :timestamp="activity.time">
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata.js'
import logistics from './Logistics.js'
export default {
  name: 'Order',
  data() {
    return {
      ordersQuery: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      orderList: [],
      dialogFormVisible: false,
      editAddress: {
        address1: [],
        address2: ''
      },
      editAddressRules: {
        address1: [{ required: true, message: '请选择省市区/县', trigger: 'blur' }],
        address2: [{ required: true, message: '请填写详细地址', trigger: 'blur' }]
      },
      cityData,
      logisticsInfo: logistics[0].logisticsInfo,
      dialogLogVisible: false
    }
  },
  created() {
    this.getOrdersList()
  },
  methods: {
    async getOrdersList() {
      const { data: res } = await this.$http.get('orders', { params: this.ordersQuery })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.orderList = res.data.goods
      this.total = res.data.total
      console.log(this.logisticsInfo)
    },
    searchOrder() {},
    handleEdit(row) {
      this.dialogFormVisible = true
    },

    handleSizeChange(val) {
      this.ordersQuery.pagesize = val
      this.getOrdersList()
    },
    handleCurrentChange(val) {
      this.ordersQuery.pagenum = val
      this.getOrdersList()
    },
    editForm() {
      this.$refs.editAddress.validate(valid => {
        if (!valid) return
      })
      this.closeForm()
    },
    closeForm() {
      this.dialogFormVisible = false
      this.$refs.editAddressRef.resetFields()
    },
    showLogistics() {
      this.dialogLogVisible = true
    }
  }
}
</script>

<style lang="less" scoped>
@import '../../plugins/timeline/timeline.css';
@import '../../plugins/timeline-item/timeline-item.css';
.el-table {
  font-size: 5px;
}
.el-cascader {
  width: 100%;
}
</style>
